package mp1.Controller;



import mp1.entity.Product;
import mp1.entity.Supplier;
import mp1.exception.ProductAdminException;
import mp1.service.ProductService;
import mp1.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    SupplierService supplierService;


    @GetMapping("getSupplier/{id}")
    public ResponseEntity<Supplier> getSupplier(@PathVariable long id){
        Supplier result = supplierService.findSupById(id);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }



    @GetMapping("")
    public ResponseEntity<List<Supplier>> getSupplierByProName(@RequestBody String proName){
        List<Supplier> result = supplierService.findSupByPro(proName);
        if(result==null) {
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(result);
        }
    }



    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('supplier/update')")
    public void deleteSupplier(@PathVariable long id) throws ProductAdminException {
        supplierService.deleteSupById(id);
    }


    @PostMapping("")
    @PreAuthorize("hasAuthority('supplier/update')")
    public void addSupplier(@RequestBody Supplier supplier) throws ProductAdminException {
        supplierService.addSup(supplier);
    }


    @PutMapping("")
    @PreAuthorize("hasAuthority('supplier/update')")
    public void updateSupplier(@RequestBody Supplier supplier) throws ProductAdminException {
        supplierService.updateSup(supplier);
    }
}
